package com.yanzhiyi.user.controller;


import com.github.pagehelper.Page;
import com.yanzhiyi.user.pojo.Role;
import com.yanzhiyi.user.entity.PageResult;
import com.yanzhiyi.user.entity.Result;
import com.yanzhiyi.user.entity.StatusCode;
import com.yanzhiyi.user.pojo.SearchCondition;
import com.yanzhiyi.user.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * FileName: RoleController
 * Author:   xuan zongjun
 * Date:     2020/10/21 15:31
 * Description: 角色控制层
 */

@RestController
@CrossOrigin
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;


    /**
     * 添加角色  使用
     * @param role
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Role role,@RequestParam("menus") Integer [] menus,@RequestParam("permissionIds") Integer [] permissionIds){
        roleService.add(role,menus,permissionIds );
        return new Result( true,StatusCode.OK,"添加成功" );
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Role role,@RequestParam("menus") Integer [] menus,@RequestParam("permissionIds") Integer [] permissionIds){
        roleService.update( role,menus,permissionIds );
        return new Result( true,StatusCode.OK,"修改成功" );
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(Integer id) {
        roleService.delete( id );
        return new Result( true,StatusCode.OK,"删除成功" );
    }


    /**
     * 根据id 查询角色  使用
     * @param id
     * @return
     */
    @PostMapping("/findById")
    public Result<Map<String,Object>>  findById(Integer id){
        Map<String,Object> map = roleService.findById( id );
        return new Result<Map<String,Object>>(true,StatusCode.OK,"查询成功",map  );
    }



    /**
     * 分页和多条件查询  使用
     * @param name
     * @param start
     * @param size
     * @return
     */
    @PostMapping("/searchPage")
    public Result<PageResult<Role>> searchPage( @RequestParam(value = "name",required = false) String  name, @RequestParam int start, @RequestParam int size){
        SearchCondition searchCondition = new SearchCondition();
        searchCondition.setName( name );
        Page<Role> page = roleService.searchPage( searchCondition,start, size );
        PageResult<Role> pageResult = new PageResult<>( page.getTotal(),page.getResult() );
        Result<PageResult<Role>> result = new Result(true,StatusCode.OK,"查询成功",pageResult);
        return result;
    }



    /**
     * 查询所有的角色
     * @return
     */
    @GetMapping("/findAll")
    public Result<Role> findAll(){
        List<Role> roleList = roleService.findAll();
        return new Result<Role>(true, StatusCode.OK,"获取成功",roleList);
    }


    /**
     * 分页查询
     * @param start
     * @param size
     * @return
     */
    @PostMapping("/findPage")
    public Result<Role> findPage(int start, int size){
        Page<Role> page = roleService.findPage( start, size );
        PageResult<Role> pageResult = new PageResult<>( page.getTotal(),page.getResult() );
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 分页和多条件查询
     * @param searchMap
     * @param start
     * @param size
     * @return
     */
    @GetMapping("/searchPage/{start}/{size}")
    public Result<Role> findPage(@RequestParam Map<String, Object> searchMap, @PathVariable int start, @PathVariable int size){
        Page<Role> page = roleService.findPage( searchMap,start, size );
        PageResult<Role> pageResult = new PageResult<>( page.getTotal(),page.getResult() );
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 多条件查询
     * @param searchMap
     * @return
     */
    @PostMapping("/search")
    public Result<List<Role>> findList(Map<String, Object> searchMap) {
        List<Role> list = roleService.findList( searchMap );
        return new Result<List<Role>>( true,StatusCode.OK,"查询成功" ,list);
    }






}
